package Data_IO.data_transformation;

import java.io.*;
import com.alibaba.fastjson.*;

import javax.tools.Tool;

public class JsonArray2file {

    /**
     *
     * @param jsonArray pathName
     * @throws IOException
     */
    public static void json2file(JSONArray jsonArray, String pathName) throws IOException {

        Tools tool = new Tools();
        File file = new File(pathName);//identify the file path
        if(!file.exists())//Determine if the file exists, and if not, create a new one
        {
            file.createNewFile();
        }
        FileOutputStream fileOutputStream=new FileOutputStream(file);
        OutputStreamWriter outputStreamWriter=new OutputStreamWriter(fileOutputStream,"utf-8");
        BufferedWriter bufferedWriter= new BufferedWriter(outputStreamWriter);//Create a character buffered output stream object

        String jsonString = jsonArray.toString();
        String JsonString = tool.stringToJSON(jsonString);
        bufferedWriter.write(JsonString);//Writing formatted jsonarray strings to a file
        bufferedWriter.flush();//Empty the buffer and force the data to be output
        bufferedWriter.close();
    }
}
